package org.example.leetCode;

/**
 * @Author Qinkai
 * @Description TODO $
 * @Date 2021-09-13
 **/
public class FindMedianSortedArrays {
    public static void main(String[] args) {
        int[] num1 = {1, 3};
        int[] num2 = {2, 4};
        FindMedianSortedArrays findMedianSortedArrays = new FindMedianSortedArrays();
        double medianSortedArrays = findMedianSortedArrays.findMedianSortedArrays(num1, num2);
        System.out.println(medianSortedArrays);
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        if (nums1.length < 1 && nums2.length < 1) {
            return 0;
        }
        int m = 0;
        int n = 0;
        int[] num = new int[nums1.length + nums2.length];
        for (int i = 0; i < nums1.length + nums2.length; i++) {

            if (m == nums1.length) {
                num[i] = nums2[n++];
                continue;
            } else if (n == nums2.length) {
                num[i] = nums1[m++];
                continue;
            }
            if (nums1[m] < nums2[n]) {
                num[i] = nums1[m++];
            } else {
                num[i] = nums2[n++];
            }
        }
        int i = num.length / 2;
        if (num.length % 2 == 0) {
            return ((double) num[i] + (double) num[i - 1]) / 2;
        } else {
            return num[i];
        }
    }
}
